/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2006，所有权利保留。
 * 
 * 文件名：	ProcessLogMgr.java
 * 模块说明：	
 * 修改历史：
 * 2006-11-10 - lxm - 创建。
 */
package com.hd123.rumba.log;

import java.util.List;

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;

import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.runtime.IAfterSaveNew;
import com.hd123.rumba.runtime.IBeforeRemove;
import com.hd123.rumba.runtime.IBeforeSave;
import com.hd123.rumba.runtime.Manager;
import com.hd123.rumba.runtime.ObserverRegistry;

/**
 * 处理过程日志 | 管理员 | 实现类
 * 
 * @author lxm
 * 
 */
public class ProcessLogMgr extends Manager implements IProcessLogMgr {

  public static final String CAPTION_MANAGER = "提示信息管理员";

  public ProcessLogMgr() {
    super();
    this.registryClass(IProcessLog.class.getName(), ProcessLog.class.getName());
  }

  public IProcessLog createLog() throws BusinessException {
    return new ProcessLog();
  }

  public void saveT(IProcessLog log) throws BusinessException, HibernateException {
    this.checkParameterNotNull(log, "log");
//    Session ses = HibernateSession.sessionFactory.openSession();
    Session ses = getSession();
    Transaction tx = ses.beginTransaction();
    try {
      ObserverRegistry.notifyObservers(IBeforeSave.class, log);
      ses.saveOrUpdate(log);
      ObserverRegistry.notifyObservers(IAfterSaveNew.class, log);
      tx.commit();
    } catch (Exception e) {
      tx.rollback();
      throw new BusinessException(e.getMessage());
    } finally {
      ses.flush();
//      ses.close();
    }
  }

  public void remove(IProcessLog log) throws BusinessException, HibernateException {
    this.checkParameterNotNull(log, "log");
    ObserverRegistry.notifyObservers(IBeforeRemove.class, log);
    getSession().delete(log);
  }

  public IProcessLog getLog(String uuid) throws BusinessException, HibernateException {
    this.checkParameterNotNull(uuid, "uuid");
    return (IProcessLog) getSession().get(IProcessLog.class, uuid);
  }

  public int getMaxItemNo(String processUuid) throws BusinessException, HibernateException {
    this.checkParameterNotNull(processUuid, "processUuid");
    List list = getSession().find("select max(pl.itemNo) from " + IProcessLog.class.getName() + " pl"
        + " where pl.processUuid = ?", processUuid, Hibernate.STRING);
    Integer result = list.size() > 0 ? (Integer) list.get(0) : null;
    return result == null ? -1 : result.intValue();
  }
  
}
